package com.zhuoshi.sevlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhuoshi.bean.ManagerBean;
import com.zhuoshi.dao.ManagerDao;
import com.zhuoshi.utils.DESEncrypt;

@SuppressWarnings("serial")
public class CheckLogin extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public CheckLogin() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password").trim();
		String verifycode = request.getParameter("verifyCode").trim();//获取前台页面传递过来的输入的验证码
		HttpSession session = request.getSession();
		String rcode = session.getAttribute("rCode").toString();

		if(verifycode.equals(rcode)){
			
		
		ManagerDao dao = new ManagerDao();
		ManagerBean bean = dao.checkLogin(username, DESEncrypt.encryptMD5(password));
		if(bean!=null){
//			HttpSession session = request.getSession();
			session.setAttribute("loginBean", bean);
			session.setMaxInactiveInterval(3600);//设置失效时间为3600秒  也就是1小时
			response.sendRedirect("index.jsp");
		}else{
			response.getWriter().write("<script language='javascript'>alert('用户名或者密码错误！！！');location.href='login.jsp';</script>");
		}
		}else{
			response.getWriter().write("<script language='javascript'>alert('验证码错误！！！');location.href='login.jsp';</script>");
		}
		
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
